package com.yidu.labyrinth.dao;
import com.yidu.labyrinth.main.LabyrinthModuleEntity;
import com.yidu.labyrinth.main.LabyrinthModuleOrRoleEntity;
import com.yidu.labyrinth.main.LabyrinthRoleEntity;
import com.yidu.labyrinth.vo.LabyrinthVo;
import com.yidu.labyrinth.vo.Pages;
import org.hibernate.HibernateException;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.util.StringUtils;
import java.util.List;

/**
 * @program: labyrinth
 * @description  角色模块数据层实现类
 * @Date:2019-09-25 13:44
 * @Author:谭俊
 **/

public class LabyrinthRoleDaoImpl implements LabyrinthRoleDao {

    private HibernateTemplate hibernateTemplate;

    public HibernateTemplate getHibernateTemplate() {
        return hibernateTemplate;
    }

    public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
        this.hibernateTemplate = hibernateTemplate;
    }

    /**
     * 查询所有
     * @param labyrinthRoleEntity
     * @return
     */
    @Override
    public List<LabyrinthRoleEntity> findAll(LabyrinthRoleEntity labyrinthRoleEntity, Pages pages) {

       // DetachedCriteria
        DetachedCriteria criteria = DetachedCriteria.forClass(LabyrinthRoleEntity.class);

        //条件搜索
        //按模块名称模糊搜索
        if (!StringUtils.isEmpty(labyrinthRoleEntity.getRoleName())) {
            criteria.add(Restrictions.ilike("roleName", "%" + labyrinthRoleEntity.getRoleName() + "%"));
        }

         //pages.getPage(),pages.getLimit()
        List<LabyrinthRoleEntity> list = ( List<LabyrinthRoleEntity>)hibernateTemplate.findByCriteria(criteria,pages.getOutPage(),pages.getLimit());

        System.out.println(pages.getOutPage()+pages.getLimit()+"行");

        return list;
    }

    /**
     * 查询总行数
     * @param labyrinthVo Vo模型层
     * @return
     */
    @Override
    public Long count(LabyrinthVo labyrinthVo) {

        DetachedCriteria criteria = DetachedCriteria.forClass(LabyrinthRoleEntity.class);

        criteria.setProjection(Projections.rowCount());

        //条件搜索
        //按模块名称模糊搜索
        if (!StringUtils.isEmpty(labyrinthVo.getRoleName())) {
            criteria.add(Restrictions.ilike("roleName", "%" + labyrinthVo.getRoleName() + "%"));
        }

        Long count = (Long)hibernateTemplate.findByCriteria(criteria).get(0);

        System.out.println(count+"总行数");


        return count;
    }





    @Override
    public int save(LabyrinthRoleEntity labyrinthRoleEntity) {
        try {
            hibernateTemplate.saveOrUpdate(labyrinthRoleEntity);
        }catch (HibernateException e){
            return 0;
        }

        return 1;
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @Override
    public LabyrinthRoleEntity findById(String id) {

        LabyrinthRoleEntity labyrinthRoleEntity=hibernateTemplate.get(LabyrinthRoleEntity.class,id);

        System.out.println(labyrinthRoleEntity.getRoleName()+labyrinthRoleEntity.getRoleCreateDate()+"时间");

        return labyrinthRoleEntity;

    }






    @Override
    public List<LabyrinthModuleEntity> findAlls() {

        DetachedCriteria detachedCriteria = DetachedCriteria.forClass(LabyrinthModuleEntity.class);
        List<LabyrinthModuleEntity> list = (List<LabyrinthModuleEntity>) hibernateTemplate.findByCriteria(detachedCriteria);

        System.out.println(list.size()+"dao集合");

        return list;
    }




}